import {
    click_first_menu,
    click_second_menu,
    request_sytem_data_tag,
    save_system_type_data,
    update_system_data
} from "../../body/contants";
import axios from "axios";
import {SystemTreeUrl} from "../../../common/API";


export const requestSystemTypeData = () => {
    return (dispatch) => {
        axios.get(SystemTreeUrl)
            .then((res) => {
                const fList = res.data.data;
                const sList = fList[0].children;
                const action = saveSystemTypeData(fList, sList);
                dispatch(action);
                const cid = sList[0].id;
                const cidAction = requestSystemArticleData(cid,0);
                dispatch(cidAction);
            })
    }
};
//   firstChoice:0,
//     secondChoice:0,
const saveSystemTypeData = (fList, sList) => (
    {
        type: save_system_type_data,
        systemTypeFirstData: fList,
        systemTypeSecondData: sList
    }
);

/**
 *@param sList 第二个列表的数据
 * @param fIndex 点击位置
 */
export const clickFirstMenu = (fIndex, sList) => ({
    type: click_first_menu,
    firstChoice: fIndex,
    systemTypeSecondData: sList
});

export const clickSecondMenu = (sIndex) => ({
    type: click_second_menu,
    secondChoice: sIndex
});


export const requestSystemArticleData = (cid, pageIndex) => {
    return (dispatch) => {
        const url = `/article/list/${pageIndex}/json?cid=${cid}`;
        axios.get(url)
            .then((res) => {
                const action = updateSystemArticleData(res.data.data.datas, cid,pageIndex);
                dispatch(action);

                const tagAction = canRequestSystemData(true);
                dispatch(tagAction);
            }).catch((e) => {

        })
    }
};

export const updateSystemArticleData = (data, cid,pageIndex) => ({
    type: update_system_data,
    systemArtList: data,
    pageIndex: pageIndex,
    cid:cid,
});

export const canRequestSystemData = (bool) => ({
    type: request_sytem_data_tag,
    bool: bool
});